<template>
  <el-dialog :visible.sync="dialogTableVisible" :append-to-body="true" :lock-scroll="false" :title="isEdit?'Edit':'Create'" width="900px" @close="handleCloseDetailDialog">
    <el-form ref="postForm" :model="postForm" :rules="rules" label-position="left" label-width="70px" style="margin-left:50px;">

      <!--<el-form-item :label="$t('table.area')" required>-->
      <!--<el-select v-model="postForm.area" class="filter-item" placeholder="Please select">-->
      <!--<el-option v-for="(item, index) in areaOptions" :key="index" :label="item.name" :value="item.id" />-->
      <!--</el-select>-->
      <!--</el-form-item>-->

      <el-form-item :label="$t('table.available')" prop="is_available">
        <el-radio-group v-model="postForm.is_available">
          <el-radio :label="1">{{ $t('table_element.available.on') }}</el-radio>
          <el-radio :label="0">{{ $t('table_element.available.un') }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <template>
        <el-tabs v-model="activeName">
          <el-tab-pane label="en" name="first" >
            <el-form-item :label="$t('table.name')" required>
              <el-input v-model="postForm.en_name" name="name" />
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="zh-ch" name="second">
            <el-form-item :label="$t('table.name')">
              <el-input v-model="postForm.cn_name" name="cnname" />
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="zh-hk" name="third">
            <el-form-item :label="$t('table.name')">
              <el-input v-model="postForm.hk_name" name="hkname" />
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="zh-tw" name="fourth">
            <el-form-item :label="$t('table.name')">
              <el-input v-model="postForm.tw_name" name="twname" />
            </el-form-item>
          </el-tab-pane>
        </el-tabs>
      </template>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCloseDetailDialog">{{ $t('table.cancel') }}</el-button>
      <el-button type="primary" @click="submitForm('postForm')">{{ $t('table.submit') }}</el-button>
    </div>

  </el-dialog>

</template>
<script>

import articleCategoryApi from '@/api/article/articlecategory'

const defaultForm = {
  en_name: '',
  cn_name: '',
  hk_name: '',
  tw_name: '',
  is_available: 0,
  area: ''

}

export default {
  name: 'Detail',
  data() {
    return {
      activeName: 'first',
      postForm: Object.assign({}, defaultForm),
      rules: {
        name: [{ required: true, message: 'name is required', trigger: 'blur' }]
      },
      areaList: [],
      imageUrl: '',
      recordid: 0,
      isEdit: false,
      dialogTableVisible: false
    }
  },
  computed: {
    lang() {
      return this.$store.getters.language
    },
    areaOptions: function() {
      return this.$store.state.locale.areas
    }
  },
  created() {
    if (this.isEdit) {
      const id = this.editId || (this.$route.params && this.$route.params.id)
      this.showRecord(id)
    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
  },

  mounted() {

  },

  methods: {
    showRecord(id) {
      articleCategoryApi.showRecord(id).then(response => {
        const serviceResponse = response.data
        if (serviceResponse.code === 0) {
          this.postForm = serviceResponse.data.record
        }
      }).catch(err => {
        console.log(err)
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.editId > 0) {
            articleCategoryApi.updateRecord(this.editId, this.postForm).then(response => {
              const serviceResponse = response.data
              if (serviceResponse.code === 0) {
                this.$message({
                  message: 'success',
                  type: 'success'
                })
                this.handleCloseDetailDialog()
              } else {
                this.$message({
                  message: 'error',
                  type: 'error'
                })
              }
            })
          } else {
            articleCategoryApi.storeRecord(this.postForm).then(response => {
              const serviceResponse = response.data
              if (serviceResponse.code === 0) {
                this.$message({
                  message: 'success',
                  type: 'success'
                })
                this.handleCloseDetailDialog()
              } else {
                this.$message({
                  message: 'error',
                  type: 'error'
                })
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },

    handleImagesSuccess(res, file) {
      if (res.code === 0) {
        this.postForm.ad_image = res.data.paths[0].path
        const imgpath = res.data.imgpath
        this.imageUrl = imgpath + res.data.paths[0].path
      } else {
        alert(res.data.msg)
      }
    },
    handShowDetailDialog(id) {
      if (id > 0) {
        this.isEdit = true
        this.editId = id
        this.showRecord(id)
      } else {
        this.isEdit = false
        this.editId = 0
        this.postForm = Object.assign({}, defaultForm)
      }

      this.dialogTableVisible = true
    },
    handleCloseDetailDialog() {
      this.resetForm('postForm')
      this.editId = 0
      this.$emit('indexRecord')
      this.$store.dispatch('setArticleCategory').then(res => {})
      this.activeName = 'first'
      this.dialogTableVisible = false
    }

  }

}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    max-width: 178px;
    max-height: 178px;
    display: block;
  }
</style>
